package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.BrandVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandService brandService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()

        );

        return new PageUtils(page);
    }



    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
//        前端上送brand_id 和 catelog_id
//        保存字段  brand_id、catelog_id brand_name、catelog_name
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        //修改品牌管理时会直接修改brand表的数据，但是存在关联表的brand数据不会更新，所有这里同步更新pms_category_brand_relation的brandId，name
        //这里只需要修改品牌名，修改菜单只会动品牌名，不会动分类名
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
//        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(catId != null,"catlog_id",catId);
//        if (this.count(queryWrapper) > 0){
//            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
//            categoryBrandRelationEntity.setCatelogId(catId);
//            categoryBrandRelationEntity.setCatelogName(name);
//            this.update(categoryBrandRelationEntity,queryWrapper);
//        }
        this.baseMapper.updateCategory(catId,name);

    }

    @Override
    public List<BrandEntity> getBrandRelationList(Long catId) {
        List<CategoryBrandRelationEntity> brandRelationEntityList = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //返回brand品牌的详情
        List<BrandEntity> collect = brandRelationEntityList.stream().map(item -> {
            //可能会查不到，如果使用增强for做会出现null的情况吗?
            BrandEntity brandEntity= brandService.getById(item.getBrandId());
            if (brandEntity == null){
                return null;
            }
            return brandEntity;
            //筛选调为null的对象
        })/*.filter(brandEntity -> brandEntity != null)*/.collect(Collectors.toList());
        return collect;
    }


}